<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>

      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <template>
            <el-submenu :index="item.id+'' " v-for="item in menulist" :key="item.id">
              <template slot="title">
                <!-- 图标   -->
                <i :class="iconsObj[item.id]"></i>
                <!-- 文本   -->
                <span>{{item.authName}}</span>
              </template>

              <!-- 二级菜单 -->
              <template>
                <el-submenu :index=" '/'">
                  <template slot="title">
                    <!-- 图标 -->
                    <i class="el-icon-menu"></i>
                    <!-- 文本 -->
                    <span>二级菜单</span>
                  </template>

                  <!-- 三级菜单 -->
                  <el-menu-item
                    :index=" '/' + subItem.path"
                    v-for="(subItem,index)  in item.children"  
                    :key="subItem.id"
                    @click="saveNavState('/' + subItem.path,subItem.authName,subItem.id,index)"
                  >
                    <template slot="title">
                      <!-- 图标 -->
                      <i class="el-icon-menu"></i>
                      <!-- 文本 -->
                      <span>{{subItem.authName}} </span>
                    </template>
                  </el-menu-item>
                </el-submenu>
              </template>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->

      <el-main>
        <el-tabs
          v-model="editableTabsValue"
          type="card"
          editable
          @edit="handleTabsEdit"
        >
          <el-tab-pane
                
                 :tab-click="tabClick(activeId,index )"
            :key="item.name"
            v-for="(item, index) in editableTabs"
            :label="item.title"
            :name="item.name"
            :path="item.path"  
          > 
            <tab-component :is="item.content" ></tab-component>
          </el-tab-pane>
        </el-tabs>
<!-- <Welcome/> -->
        <!-- 路由占位符 -->
        <!-- <router-view></router-view> -->
      </el-main>
    </el-container>
  </el-container>
</template>
 
<script>
import Login from "../components/Login.vue";
import Home from "../components/Home.vue";
import Welcome from "../components/Welcome.vue";
import Users from "../components/user/Users.vue";
import Rights from "../components/power/Rights.vue";
import Roles from "../components/power/Roles.vue";
import Categories from "../components/goods/Cate.vue";
import Params from "../components/goods/Params.vue";
import Goods from "../components/goods/List.vue";
 
import Orders from "../components/order/Order.vue";
import Reports from "../components/report/Report.vue";

export default {
  data() {
    return {
      //左侧菜单数据
      menulist: [],
      iconsObj: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },

      //是否折叠
      isCollapse: false,
      //被激活的链接地址
      activePath: "",
      activeAuthName: "",
      activeId: '',
      editableTabsValue: '',
      editableTabs: [
        // {
        //   title: "菜单栏",
        //   name: "",
        //   content: "22",
        //   path:'',
        //   id:''
        // },
      ],
      tabIndex:0
    };
  },
  components: {
    Login,
    Home,
    Welcome,
    Users,
    Rights,
    Roles,
    Categories,
    Params,
    Goods,
    Orders,
    Reports
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
    this.activeAuthName = window.sessionStorage.getItem("activeAuthName");
    this.activeId = window.sessionStorage.getItem("activeId");
  },

  methods: {
 

    handleTabsEdit(targetName, action) {
      if (action === "add") {
        let newTabName = ++this.tabIndex + "";
        this.editableTabs.push({
          title: "New Tab",
          name: newTabName,
          content: "New Tab content"
        });
        this.editableTabsValue = newTabName;
      }
      if (action === "remove") {
        if (action === "remove") {
          let tabs = this.editableTabs;
          let activeName = this.editableTabsValue;
          // 长度大于等于2时移除元素并删除
          if (tabs.length >= 2) {
            if (activeName === targetName) {
              tabs.forEach((tab, index) => {
                if (tab.name === targetName) {
                  let nextTab = tabs[index + 1] || tabs[index - 1];
                  if (nextTab) {
                    activeName = nextTab.name;
                  }
                }
              });
            }
            this.editableTabsValue = activeName;
            this.editableTabs = tabs.filter(tab => tab.name !== targetName);
          } else {
            if (activeName === targetName) {
              tabs.forEach((tab, index) => {
                if (tab.name === targetName) {
                  let nextTab = tabs[index + 1] || tabs[index - 1];
                  if (nextTab) {
                    activeName = nextTab.name;
                  }
                }
              });
            }
            this.editableTabsValue = activeName;
            this.editableTabs = tabs.filter(tab => tab.name !== targetName); //过滤移除
            this.isCollapse = true; //折叠侧边栏
          }
        }
      }
    },

    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
      console.log(this.menulist);
    },
    //点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    //保存链接的激活状态
    async saveNavState(activePath, activeAuthName, activeId, index) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;

      window.sessionStorage.setItem("activeAuthName", activeAuthName);
      this.activeAuthName = activeAuthName;

      window.sessionStorage.setItem("activeId", activeId);
      this.activeId = activeId;

      if (this.editableTabs == undefined || this.editableTabs.length <= 0) {
        let newTabName = ++this.tabIndex + "";
        this.editableTabs.push({
          title: this.activeAuthName,
          id: this.activeId,
          path: this.activePath,
          index:index,
          name: newTabName,
          content:
            this.activePath
              .substr(1)
              .slice(0, 1)
              .toUpperCase() + this.activePath.substr(1).slice(1)
        });
        this.editableTabsValue = newTabName;
      } else {
        let id = 0;
        let index = 0;
        for (let i = 0; i < this.editableTabs.length; i++) {
          let Idd = this.editableTabs[i].id;
          if (Idd == this.activeId) {
            id = Idd;
            index  = i;
          }
        }
        if (id == 0) {
          let newTabName = ++this.tabIndex + "";
          this.editableTabs.push({
            title: this.activeAuthName,
            id: this.activeId,
            path: this.activePath,
            name: this.editableTabs.length + 1 + "",
              index:index,
            content:
              this.activePath
                .substr(1)
                .slice(0, 1)
                .toUpperCase() + this.activePath.substr(1).slice(1)
          });
          this.editableTabsValue = newTabName;
        } else {
           this.editableTabsValue = ++ index +''
        }
      }
    },
       tabClick(activeId,index ) {
        //  console.log(index);
         
         var arr,arr2,arId=0
           for(let j = 0; j<this.menulist.length; j++){
                arr = this.menulist[j].children
              for(let q = 0; q < arr.length; q++) {
                arr2 = arr[q].id;
                // if(arr2 == activeId){
                //   arId = arr2
             
                // }
              } 
           }
        //       if (arId== 0) {
        //   alert(33)
        // }
          // console.log(arr2);
    },
  },
 
};
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: white;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.red {
  color: red;
}
</style>